library(gridExtra)
library(tidyverse)
library(scales)

# Data preparation
size <- sort(c(2058, 66175, 213422, 590000, 1000000, 357, 21130, 52300, 9458, 12179, 
               1439, 6411694, 1600000, 1000, 19823, 7800, 452616, 185300, 292845600, 
               200, NA, 595169, 1124, 11659, 170000, 25394, 25394, 1545, 96302, 
               1500000, 2000, 1645760, 3823, NA, 382, 13026, 59020, 16000, 174778, 
               126120, 1000, NA, 2093, 1512, 198170, 1000000, 500000, 120000, 
               137055, 40754, 2800, 23058, 221, 668, 2000000, 7500, 11659, 100000, 
               302506))

size_cleaned <- na.omit(size)

data <- data.frame(size = size_cleaned)

# Plotting the histogram
p1 <- ggplot(data, aes(x = log10(size))) +
  geom_histogram(bins = 20, fill = "black", color = "black") +
  labs(title = "Histogram of dataset sizes of visual social science papers",
       y = "Number of papers",
       x = "Number of images/frames studied") +
  theme_bw() +
  scale_x_continuous(breaks = c(2,4,6,8),
                     labels = c("100",  "10,000", "1,000,000", "100,000,000"))  +
  theme(
    plot.title = element_text(size = 15), # Increase title font size
    axis.title = element_text(size = 14),               # Increase axis title font size
    axis.text = element_text(size = 14),                # Increase axis text font size
    strip.text = element_text(size = 14),               # Increase facet label font size
  ) 

# Visual medium data
visualmedium <- data.frame(type = c("Social media", "Television","Newspaper/magazine", "Other", "Satellite/street", 
                                     "Unedited footage"), 
                           len = c( 25, 14, 11, 3, 2, 2))

# Fixing column names for the table
colnames(visualmedium) <- c("Visual medium of data source", "Number of papers")



# Creating the table with adjusted size
p2 <- tableGrob(visualmedium, rows = NULL, 
                theme = ttheme_default(core = list(fg_params = list(hjust = 0, x = 0.1, cex = 1.2)),
                                       colhead = list(fg_params = list(hjust = 0.5, cex = 1.2))))

# Adjusting the size of the table
p2 <- gtable_add_padding(p2, padding = unit(c(1.5, 1.5, 1.5, 1.5), "mm"))

# Arranging the plots
grid.arrange(p1, p2, ncol = 2, widths = c(1.3, 1))
